package dev.ece.suit.hexmap;

import com.bulletphysics.collision.shapes.CollisionShape;
import com.bulletphysics.dynamics.RigidBody;

import dev.ece.core.cells.events.Mouse3D;
import dev.ece.core.shader.DepthShader;
import dev.ece.core.sprite.Sprite;
import dev.ece.util.jbullet.EceJbulletWorld;
import dev.ece.util.lib.Vector2f;
import dev.ece.util.lib.Vector3f;
import dev.ece.util.tools.Camera;

@Deprecated
public class HexMapSprite extends Sprite<Float, HexMapModel> implements Mouse3D {

	private DepthShader depthShader = DepthShader.getDepthShader();
	
	private float offset;
	
	private HexMapListener hexMapListener;
	
	private RigidBody rigidBody;
	
	private boolean autoVisible = true;
	
	public HexMapSprite(HexMapModel model) {
		this(model, 10f);
	}
	
	public HexMapSprite(HexMapModel model, float distance) {
		super(model);
		this.setValue(distance);
	}
	
	@Override
	public void onLogic(Camera camera) {
		if(this.isAutoVisible()) {
			Vector3f pos = camera.position;
			Vector3f center = camera.center;
			HexMapChunks chunks = this.getModel().getChunks();
			//计算摄像机XZ平面上的正前方
			Vector2f dir = new Vector2f();
			dir.splus(new Vector2f(pos.getX(), pos.getZ()), new Vector2f(center.getX(), center.getZ()));
			dir.normalize();
			dir.setLength(this.getOffset());
			chunks.setAutoVisible(pos.getX() + dir.getX(), pos.getZ() + dir.getY() , this.getValue());
		}
	}

	@Override
	public void onRenderDepthMap(Camera camera) {
		depthShader.glUseProgram();
		depthShader.glModelViewProjectMatrix();
		super.onRenderDepthMap(camera);
	}

	public RigidBody addToEceJbulletWorld(EceJbulletWorld world) {
		removeFromEceJbulletWorld(world);
		CollisionShape shape = this.getModel().createCollisionShape();
		rigidBody = world.addRigidBody(shape, 0);
		rigidBody.setUserPointer(this);
		return rigidBody;
	}
	
	public void removeFromEceJbulletWorld(EceJbulletWorld world) {
		if(rigidBody != null) {
			world.removeRigidBody(rigidBody);
			rigidBody = null;
		}
	}
	
	public float getOffset() {
		return offset;
	}

	@Override
	public boolean isMouseInCell(Vector3f position, Vector3f dir) {
		return true;
	}

	protected boolean isIntersected(Vector3f position, Vector3f dir, HexMapPickUp hexMapPickUp) {
		return this.getModel().isIntersected(position, dir, hexMapPickUp);
	}
	
	@Override
	public boolean onMouseButtonClick(int button, Vector3f position, Vector3f dir) {
		HexMapPickUp hexMapPickUp = new HexMapPickUp();
		if(isIntersected(position, dir, hexMapPickUp)) {
			if(hexMapPickUp.getCell() != null) {
				if(hexMapListener != null) {
					hexMapListener.onHexMapCellClicked(hexMapPickUp.getCell());
				}
				return true;
			}
		}
		return false;
	}

	@Override
	public boolean isNormalRender() {
		return true;
	}

	@Override
	public boolean isAlphaRender() {
		return true;
	}

	@Override
	public void setValue(Float value) {
		super.setValue(value);
		this.offset = value - this.getModel().getTerrainFactory().getCellOutSize() * 2;
	}

	public HexMapListener getHexMapListener() {
		return hexMapListener;
	}

	public void setHexMapListener(HexMapListener hexMapListener) {
		this.hexMapListener = hexMapListener;
	}

	public boolean isAutoVisible() {
		return autoVisible;
	}

	public void setAutoVisible(boolean autoVisible) {
		this.autoVisible = autoVisible;
		if(!autoVisible) {
			HexMapChunks chunks = this.getModel().getChunks();
			chunks.showAllChunk();
		}
	}

}
